import { plugin } from '../../components/index.js'
import ArtifactFactory from '../../model/ArtifactFactory.js'

export class ArtifactDropTest extends plugin {
  constructor() {
    super({
      name: '修仙_圣遗物测试',
      dsc: '圣遗物掉落测试指令',
      event: 'message',
      priority: 1000,
      rule: [
        { reg: '^#修仙测试圣遗物(.*)$', fnc: 'testArtifactDrop' }
      ]
    })
  }

  async testArtifactDrop() {
    const m = this.e.msg.match(/^#修仙测试圣遗物(.*)$/)
    const arg = (m && m[1] ? m[1].trim() : '')
    const parts = ['花','羽','沙','杯','冠']
    const toks = arg.split(/\s+/).filter(Boolean)
    const partTok = toks.find(t => parts.includes(t))
    const starTok = toks.find(t => /^[1-5]$/.test(t))
    const part = partTok || parts[Math.floor(Math.random()*parts.length)]
    const star = starTok ? Number(starTok) : 2
    const item = ArtifactFactory.generateArtifact(part, star)
    const mainKeys = ['生命','防御','速度','生命加成百分比','防御加成百分比','攻击','攻击加成百分比','速度加成百分比','速度加成','暴击率','暴击伤害','首领破防百分比','元素伤害加成百分比','幸运加成百分比']
    const main = {}
    for (const k of mainKeys) if (Number(item[k])) main[k] = item[k]
    const isPercent = (key) => /百分比/.test(key) || key === '暴击率' || key === '暴击伤害' || key === '速度加成' || key === '破防加成'
    const fmt = (key, v) => isPercent(key) ? `${(Number(v||0)*100).toFixed(2)}%` : `${Number(v||0).toFixed(2)}`
    const subList = Array.isArray(item['副词条']) ? item['副词条'].map(s => `${s.键}=${fmt(s.键, s.值)}`) : []
    const dropSources = Array.isArray(item['掉落来源']) ? item['掉落来源'].join('、') : ''
    const msg = [
      `名称：${item['名称'] || ''}`,
      `部位：${item['部位'] || ''}`,
      `星级：${star}`,
      item['元素'] ? `元素：${item['元素']}` : '',
      (() => { const mk = Object.keys(main)[0]; const mv = Object.values(main)[0]; return `主词条：${mk || '无'}=${mk ? fmt(mk, mv) : '0.00'}` })(),
      subList.length ? `副词条：${subList.join('，')}` : '副词条：无',
      dropSources ? `掉落来源：${dropSources}` : ''
    ].filter(Boolean).join('\n')
    await this.e.reply(msg)
  }
}
